// components/list/list.js
const app = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    category: {
      type: Number,
      value: 0
    },
    title: {
      type: String,
      value: "首页"
    }
  },
  ready(){
    wx.setNavigationBarTitle({
      title: this.data.title
    });
    this.location();
  },
  /**
   * 组件的初始数据
   */
  data: {
    // 页码信息
    page: 1,
    pageSize: 15,

    lists: [],
    lon: '', // 当前位置 维度
    lat: '', // 当前位置 维度
    type: '1'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    selBtn(event){
      this.setData({
        type: event.currentTarget.dataset.id
      });
      this.location();
    },
    refresh(){
      this.setData({
        page: 1,
      });
      this.more();
    },
    more(){
      let params = {
        page: this.data.page,
        pageSize: this.data.pageSize,
        shop_type: this.data.category,
        type: this.data.type,
        lon: this.data.lon,
        lat: this.data.lat,
      }

      app.request('/api/shop/lists', params, (data, msg)=>{
        if (data.data.length > 0) {
          // 增加页数
          this.setData({
            page: params.page + 1,
            lists: data.data
          })
        } else {
          console.log("没有数据")
        }
      }, (msg)=>{
        console.log(msg)
      });
    },
    location(){
      let that = this;
      wx.getLocation({
        success: function(res) {
          that.setData({
            lon: res.longitude,
            lat: res.latitude
          });
        },
        complete: function(res) {
          that.refresh()
        },
      })
    }
  }
})
